//
//  ViewController.m
//  MenuScrollview
//
//  Created by zhouLiHuan on 2017/1/18.
//  Copyright © 2017年 zhouLiHuan. All rights reserved.
//

#import "ViewController.h"
#import "SubViewController.h"
#define kViewWidth [UIScreen mainScreen].bounds.size.width
#define kViewHeight  [UIScreen mainScreen].bounds.size.height
#define kLabelWidth kViewWidth/4

@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic,strong)NSMutableArray *titleArr; //字符串数组
@property (nonatomic,strong)NSMutableArray *titleLabelArr; //存放label的数组
@property (nonatomic,strong)UIScrollView *titleScorllView; //上面显示标题的view
@property (nonatomic,strong)UIScrollView *contentScrollView;
@property (nonatomic,assign)NSInteger currentPage; //当前页
@property (nonatomic,strong)UILabel *selectLabel; //选中的label

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTopTileView];
    [self creatContentScrollView];
    [self showVcView:0];
}

#pragma mark - 头部标题视图
- (void)creatTopTileView{
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64,kViewWidth, 40)];
    titleScrollView.backgroundColor = [UIColor grayColor];
    titleScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:titleScrollView];
    self.titleScorllView = titleScrollView;
    self.titleScorllView.contentSize = CGSizeMake(self.titleArr.count * kLabelWidth, 40);
    [self creatTopTitleLabel];
}

- (void)creatContentScrollView{
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.titleScorllView.bottom, kViewWidth, kViewHeight - 64 - self.titleScorllView.height)];
    contentScrollView.backgroundColor = [UIColor whiteColor];
    contentScrollView.showsHorizontalScrollIndicator = NO;
    contentScrollView.pagingEnabled = YES;
    contentScrollView.bounces = NO;
    contentScrollView.delegate = self;
    [self.view addSubview:contentScrollView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.contentScrollView = contentScrollView;
    self.contentScrollView.contentSize = CGSizeMake(kViewWidth * self.titleArr.count, kViewWidth-64-self.titleScorllView.height);
    self.contentScrollView.backgroundColor = [UIColor whiteColor];
    //添加字控制器
    [self addSubViewController];
    
}

//给头部试图添加label
- (void)creatTopTitleLabel{
    for (int i = 0 ; i < self.titleArr.count;  i++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * kLabelWidth, 0, kLabelWidth, self.titleScorllView.height)];
        titleLabel.text = self.titleArr[i];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.highlightedTextColor = [UIColor redColor];
        titleLabel.tag = i;
        [self.titleScorllView addSubview:titleLabel];
        titleLabel.userInteractionEnabled = YES;
        [self.titleLabelArr addObject:titleLabel];
        //给label添加点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(titleLabelClick:)];
        [titleLabel addGestureRecognizer:tap];
        
        //默认选中第一个
        if (i == 0) {
            _selectLabel = titleLabel;
            [self selectLabel:_selectLabel];
        }
        
    }
}


//添加子控制器
- (void)addSubViewController{
    for (int i = 0; i < self.titleArr.count; i ++) {
        SubViewController *vc = [[SubViewController alloc] init];
        [self addChildViewController:vc];
    }
}

#pragma mark - 标题label的点击事件
- (void)titleLabelClick:(UIGestureRecognizer *)recognizer{
    
    UILabel *label = (UILabel *)recognizer.view;
    
    //标题颜色变红色 变形
    [self selectLabel:label];
    
    [self titleLabelToCenter:label]; //是label居中
    
    [self contentScrollViewOffset:label.tag];

}

// 选中label
- (void)selectLabel:(UILabel *)label
{
    // 取消形变
    _selectLabel.transform = CGAffineTransformIdentity;
    // 颜色恢复
    _selectLabel.highlighted = NO;
    // 高亮
    label.highlighted = YES;
    // 形变
    label.transform = CGAffineTransformMakeScale(1.15, 1.15);
    
    _selectLabel = label;
}


//label居中方法
- (void)titleLabelToCenter:(UILabel *)selectLabel{
    
    //计算偏移量
    CGFloat offsetX = selectLabel.center.x - self.contentScrollView.width * 0.5;
    if (offsetX<0) {
        offsetX = 0;
    }
    //获取最大滚动范围
    CGFloat offsetMax = self.titleScorllView.contentSize.width - self.contentScrollView.width;
    if (offsetMax < 0) {
        offsetMax = 0;
    }
    if (offsetX > offsetMax) {
        offsetX = offsetMax;
    }
    //滚动
    [self.titleScorllView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
}

//点击标题控制器跟着滚动
- (void)contentScrollViewOffset:(NSInteger)labelTag{
    CGFloat offsetX = labelTag*kViewWidth;
    [self.contentScrollView setContentOffset:CGPointMake(offsetX, 0)];
    [self showVcView:labelTag];
}


#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //计算滑到哪一页
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    self.currentPage = index;
    for (int i = 0; i < self.titleLabelArr.count; i ++) {
        UILabel *label = self.titleLabelArr[i];
        if (i == index) {

            [self titleLabelToCenter:label];
            [self selectLabel:label];
        }
    }
    //显示控制器的view
    [self showVcView:index];
}

- (void)showVcView:(NSInteger)index{
    
    //偏移量
    CGFloat offsetX = index * self.contentScrollView.width;
    SubViewController *vc = self.childViewControllers[index];
    vc.index = index;
    if (vc.isViewLoaded) {
        return;
    }
    vc.view.frame = CGRectMake(offsetX, 0,kViewWidth, self.contentScrollView.height);

    [self.contentScrollView addSubview:vc.view];
   
    
}

#pragma mark - 懒加载
- (NSMutableArray *)titleArr{
    if (_titleArr == nil) {
        _titleArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < 10; i ++) {
            NSString *str = [NSString stringWithFormat:@"title%d",i];
            [_titleArr addObject:str];
        }
    }
    return _titleArr;
}

- (NSMutableArray *)titleLabelArr{
    if (_titleLabelArr == nil) {
        _titleLabelArr = [[NSMutableArray alloc] init];
    }
    return _titleLabelArr;
}

@end
